﻿using HuaWei;
using HuaWei.CommClass;
using HuaWei.Common;
using HuaWei.DAI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;

namespace HuaWei.CommComp
{
    public class FrmBase : Form, Interface
    {
        private bool _flag = false;
        public DataSet ds = new DataSet();
        public DataSet dsQuery = new DataSet();
        public IDataAccess ida = null;
        public Form.ControlCollection mainControl;
        public List<BindingSource> bsList = new List<BindingSource>();
        public EventManager eventManager;
        public ArrayList sqlList;
        private IContainer icontainer = null;
        public dataSets1 dataSets;

        public string OldBDID
        {
            get;
            set;
        }

        public string OldQCID
        {
            get;
            set;
        }

        public BindingSource statusBS
        {
            get;
            set;
        }

        public FrmBase()
        {
            this.InitializeComponent();
            this.OldQCID = "";
            this.OldBDID = "";
            this.eventManager = new EventManager();
            this.sqlList = new ArrayList();
        }

        public void AddEvent(object source, string objName, string eventName, EventHandler handle)
        {
            this.eventManager.DelEvent(source, objName, eventName);
            this.eventManager.AddEvent(source, objName, eventName, handle);
        }

        public virtual bool Check()
        {
            return true;
        }

        public virtual void ControlFormStatus()
        {
            if (GParams.QCInfo.QCZT >= 0)
            {
                DevCCHelp.ControlForm(base.Controls, true, 2);
            }
            else if (this.statusBS != null)
            {
                if (this.statusBS.List.Count != 0)
                {
                    string objValue = DHelp.GetObjValue(this.statusBS, "TYSB", false);
                    if ((objValue.Equals("0") ? false : !objValue.Equals("")))
                    {
                        DevCCHelp.ControlForm(base.Controls, true, 1);
                    }
                    else
                    {
                        DevCCHelp.ControlForm(base.Controls, false, 1);
                    }
                }
                else
                {
                    DevCCHelp.ControlForm(base.Controls, true, 1);
                }
            }
        }

        protected override void DefWndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 96:
                    {
                        this.LoadData();
                        this.ControlFormStatus();
                        break;
                    }
                case 97:
                    {
                        this.Save();
                        break;
                    }
                case 98:
                    {
                        this.LoadData();
                        this.ControlFormStatus();
                        break;
                    }
                case 99:
                    {
                        this.ControlFormStatus();
                        break;
                    }
                case 100:
                    {
                        this.LoadDataNew();
                        this.ControlFormStatus();
                        break;
                    }
                default:
                    {
                        base.DefWndProc(ref m);
                        break;
                    }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if ((!disposing ? false : this.icontainer != null))
            {
                this.icontainer.Dispose();
            }
            base.Dispose(disposing);
        }

        private void FrmBase_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Save();
        }

        private void FrmBase_FormClosing(object sender, FormClosingEventArgs e)
        {
        }

        private void FrmBase_Load(object sender, EventArgs e)
        {
        }

        private void InitializeComponent()
        {
            this.dataSets = new dataSets1();
            ((ISupportInitialize)this.dataSets).BeginInit();
            base.SuspendLayout();
            this.dataSets.DataSetName = "dataSets1";
            this.dataSets.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema;
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            base.ClientSize = new Size(0x124, 0x10a);
            base.Name = "FrmBase";
            this.Text = "FrmBase";
            base.FormClosing += new FormClosingEventHandler(this.FrmBase_FormClosing);
            base.FormClosed += new FormClosedEventHandler(this.FrmBase_FormClosed);
            base.Load += new EventHandler(this.FrmBase_Load);
            ((ISupportInitialize)this.dataSets).EndInit();
            base.ResumeLayout(false);
        }

        public virtual void LoadData()
        {
            if ((!this.OldBDID.Equals(GParams._BDID) || !this.OldQCID.Equals(GParams.QCInfo.QCID) ? true : GParams.CXJZ))
            {
                GParams.CXJZ = false;
                this.OldBDID = GParams._BDID;
                this.OldQCID = GParams.QCInfo.QCID;
                this.LoadingData();
                if (!this._flag)
                {
                    this.LoadedForm();
                    this._flag = true;
                }
                this.LoadedData();
                this.ControlFormStatus();
            }
        }

        public virtual void LoadDataNew()
        {
            GParams.CXJZ = false;
            this.OldBDID = GParams._BDID;
            this.OldQCID = GParams.QCInfo.QCID;
            this.LoadingData();
            if (!this._flag)
            {
                this.LoadedForm();
                this._flag = true;
            }
            this.LoadedData();
            this.ControlFormStatus();
        }

        public virtual void LoadedData()
        {
        }

        public virtual void LoadedForm()
        {
        }

        public virtual void LoadingData()
        {
            this.ida = DBHelp.IniIDA();
        }

        private void method(object sender, DataColumnChangeEventArgs e)
        {
            if (e.Column.ColumnName.Equals("TYSB"))
            {
                this.ControlFormStatus();
            }
        }

        protected void OpenDataLog(string tableName, string bdid, string qcid, string djid)
        {
            FrmDJLog frmDJLog = new FrmDJLog();
            try
            {
                frmDJLog.IniForm(tableName, bdid, qcid, djid);
                frmDJLog.ShowDialog();
            }
            finally
            {
                if (frmDJLog != null)
                {
                    ((IDisposable)frmDJLog).Dispose();
                }
            }
        }

        public void PositionChanged(object sender, EventArgs e)
        {
            this.Save();
            this.ControlFormStatus();
        }

        public void RowDeleted(object sender, DataRowChangeEventArgs e)
        {
            this.Save();
        }

        public void Save()
        {
            this.Saving();
            if (this.ds.HasChanges())
            {
                this.ida.OpenDatabase();
                this.ida.BeginTrans();
                try
                {
                    string str = string.Format("update xmqc set zt=-1 where bdid='{0}' and qcid='{1}'", GParams._BDID, GParams.QCInfo.QCID);
                    this.sqlList.Add(str);
                    if (this.sqlList.Count > 0)
                    {
                        this.ida.ExecuteNonQuery(this.sqlList, CommandType.Text);
                    }
                    this.ida.Update(this.ds, true);
                    this.ida.Commit();
                    this.sqlList.Clear();
                }
                finally
                {
                    this.ida.CloseDatabase();
                }
            }
        }

        public virtual void Saving()
        {
            foreach (BindingSource bindingSources in this.bsList)
            {
                bindingSources.EndEdit();
            }
        }

        public void SetBindingSource(BindingSource bs, string tableName, bool isSBBS = false)
        {
            this.bsList.Add(bs);
            DataTable item = this.ds.Tables[tableName];
            bs.DataSource = item;
            item.TableNewRow += new DataTableNewRowEventHandler(Class28.smethod);
            item.ColumnChanging += new DataColumnChangeEventHandler(Class39.smethod3);
            item.ColumnChanged += new DataColumnChangeEventHandler(this.method);
            item.RowDeleted += new DataRowChangeEventHandler(this.RowDeleted);
            if (isSBBS)
            {
                bs.PositionChanged += new EventHandler(this.PositionChanged);
                this.statusBS = bs;
            }
        }
    }
}